package com.oauth.global.client.ui;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.DecoratedStackPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.oauth.components.client.rpc.OAuthAsnyCallBack;
import com.oauth.global.client.beans.AppInfoBean;
import com.oauth.global.client.rpc.GlobalRemoteServiceAsync;

public class BirthdayNotification extends Composite {

	private static BirthdayNotificationUiBinder uiBinder = GWT
			.create(BirthdayNotificationUiBinder.class);

	interface BirthdayNotificationUiBinder extends
			UiBinder<DecoratedStackPanel, BirthdayNotification> {
	}

	@UiField
	DecoratedStackPanel stackPanel;

	@UiField
	VerticalPanel birthdayPanel;

	@UiField
	DeckPanel deckPanel;

	private GlobalRemoteServiceAsync mGlobalRemoteServiceAsync;

	private String applicationName = "Upcoming Birthday's";

	public BirthdayNotification() {
		initWidget(uiBinder.createAndBindUi(this));
		stackPanel.setStackText(0, "<div class=\"stackText\">" + applicationName + "</div>", true);
	}

	public void setService(GlobalRemoteServiceAsync pGlobalRemoteServiceAsync) {
		mGlobalRemoteServiceAsync = pGlobalRemoteServiceAsync;
	}

	public void initBirthdays() {
		birthdayPanel.clear();
		deckPanel.showWidget(0);

		mGlobalRemoteServiceAsync
				.getApplicationImagesForBirthday(new OAuthAsnyCallBack<List<AppInfoBean>>() {

					@Override
					public void onSuccess(List<AppInfoBean> pAppInfoList) {
						if (pAppInfoList == null || pAppInfoList.isEmpty()) {
							birthdayPanel
									.add(new HTML(
											"There are no application which features your friend's birthday.<br/>Click application link in top right corner to add more application's"));
						} else {
							for (AppInfoBean infoBean : pAppInfoList) {
								BirthdayPanel bPanel = new BirthdayPanel(infoBean,
										mGlobalRemoteServiceAsync);
								birthdayPanel.add(bPanel);
								bPanel.initBirthdays();
							}
						}
						deckPanel.showWidget(1);

					}
				});
	}
}
